﻿var shareButtons = (function ($) {
    this.addFBShareButton = function (selector) {
        // create the button
        var button = $('<a href="#"></a>');
        button.html('<img src="images/fb-share.png" />');
        
        // attaching an event listener
        button.on("click", function () {
            var sharer = "https://www.facebook.com/sharer/sharer.php?u=";
            window.open(sharer + location.href, 'sharer', 'width=626,height=436');
        });

        // appending the button to the given selector (container)
        $(selector).append(button);
        
    }

    this.addTweetButton = function (selector) {
        // creating button
        var button = $("<a></a>");
        button.attr("href", "https://twitter.com/share");
        button.attr("data-text", "Awesome Coconut Tetris!");
        button.addClass("twitter-share-button");
        button.text("Tweet");

        // copy of official twitter generated tweet script
        !function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = p + '://platform.twitter.com/widgets.js'; fjs.parentNode.insertBefore(js, fjs); } }(document, 'script', 'twitter-wjs');
        

        // appending the button to the given selector (container)
        $(selector).append(button);
    }

    this.addAllButtons = function (selector) {
        addFBShareButton(selector);
        addTweetButton(selector);
    }

    return {
        addFBShareButton: addFBShareButton,
        addTweetButton: addTweetButton,
        addAllButtons: addAllButtons
    };
})(jQuery);